package com.lifeservice.console.service.impl;

import com.lifeservice.common.core.domain.AjaxResult;
import com.lifeservice.common.utils.SnowFlakeUtil;
import com.lifeservice.common.utils.StringUtils;
import com.lifeservice.console.domain.BuildWorkClassInfo;
import com.lifeservice.console.domain.LifeCityBuilding;
import com.lifeservice.console.mapper.LifeCityBuildingMapper;
import com.lifeservice.console.mapper.LifeScheduleMapper;
import com.lifeservice.console.service.ILifeCityBuildingService;
import com.lifeservice.util.GenWxAppCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 城市大厦信息Service业务层处理
 *
 * @author lifeservice
 * @date 2023-03-01
 */
@Service
public class LifeCityBuildingServiceImpl implements ILifeCityBuildingService {
    @Autowired
    private LifeCityBuildingMapper lifeCityBuildingMapper;

    @Autowired
    private LifeScheduleMapper lifeScheduleMapper;

    @Autowired
    private GenWxAppCodeUtil genWxAppCodeUtil;

    /**
     * 查询城市大厦信息
     *
     * @param id 城市大厦信息主键
     * @return 城市大厦信息
     */
    @Override
    public LifeCityBuilding selectLifeCityBuildingById(String id) {
        return lifeCityBuildingMapper.selectLifeCityBuildingById(id);
    }

    /**
     * 查询城市大厦信息列表
     *
     * @param lifeCityBuilding 城市大厦信息
     * @return 城市大厦信息
     */
    @Override
    public List<LifeCityBuilding> selectLifeCityBuildingList(LifeCityBuilding lifeCityBuilding) {
        return lifeCityBuildingMapper.selectLifeCityBuildingList(lifeCityBuilding);
    }

    /**
     * 新增城市大厦信息
     *
     * @param lifeCityBuilding 城市大厦信息
     * @return 结果
     */
    @Override
    public int insertLifeCityBuilding(LifeCityBuilding lifeCityBuilding) {
        lifeCityBuilding.setId(SnowFlakeUtil.getInstance().nextId());
        return lifeCityBuildingMapper.insertLifeCityBuilding(lifeCityBuilding);
    }

    /**
     * 修改城市大厦信息
     *
     * @param lifeCityBuilding 城市大厦信息
     * @return 结果
     */
    @Override
    public int updateLifeCityBuilding(LifeCityBuilding lifeCityBuilding) {
        return lifeCityBuildingMapper.updateLifeCityBuilding(lifeCityBuilding);
    }

    /**
     * 批量删除城市大厦信息
     *
     * @param ids 需要删除的城市大厦信息主键
     * @return 结果
     */
    @Override
    public int deleteLifeCityBuildingByIds(String[] ids) {
        return lifeCityBuildingMapper.deleteLifeCityBuildingByIds(ids);
    }

    /**
     * 删除城市大厦信息信息
     *
     * @param id 城市大厦信息主键
     * @return 结果
     */
    @Override
    public int deleteLifeCityBuildingById(String id) {
        return lifeCityBuildingMapper.deleteLifeCityBuildingById(id);
    }

    @Override
    public AjaxResult viewWorkClass(LifeCityBuilding lifeCityBuilding) {
        lifeScheduleMapper.deleteLajiLifeWorkClass();
        lifeScheduleMapper.deleteLajiLifeSchedule();
        String buildId = lifeCityBuilding.getId();
        LifeCityBuilding build = lifeCityBuildingMapper.selectLifeCityBuildingById(buildId);
//        String url = genWxAppCodeUtil.genWechatCodeUrl(build.getTag());
        List<BuildWorkClassInfo> classList = lifeCityBuildingMapper.viewWorkClass(buildId, lifeCityBuilding.getWorkClassMonth() + "%");
        if (StringUtils.isEmpty(classList)) {
            return AjaxResult.success();
        }
        Map<String, List<BuildWorkClassInfo>> groupByDay = classList.stream().collect(Collectors.groupingBy(BuildWorkClassInfo::getServeDay));
        Map<String, List<BuildWorkClassInfo>> sortedMap = groupByDay.entrySet().stream().sorted(Map.Entry.comparingByKey(String::compareTo)).
                collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldValue, newValue) -> oldValue, LinkedHashMap::new));
        return AjaxResult.success(sortedMap);
    }
}
